import {get,post} from '../../http'
import { message } from 'antd';

const DATEILS = 'DATEILS';
const ADDDATE = 'ADDDATE';
const CHUANGBOLIST = 'CHUANGBOLIST'

const START  = 'START';
const END = 'END';
const TER = 'TER'
const RECOVERY = 'RECOVERY';


const initState={
	transportDateilsdata:{},
	addchuangbos:false
}

// reducer1
export function transportD(state=initState, action){
	switch(action.type){
		case DATEILS:
            return Object.assign({},state,{transportDateilsdata:action.data})
        case ADDDATE:
            return Object.assign({},state,{addchuangbos:action.data})
        case CHUANGBOLIST:
			return Object.assign({},state,{shipList:action.data})
		case START:
			return Object.assign({},state,{start:action.res})
		case END:
			return Object.assign({},state,{end:action.res})
		case TER:
			return Object.assign({},state,{ter:action.res})
		case RECOVERY:
			return Object.assign({},state,{recovery:action.res})
		default:
			return state
	}
} 

function startSuccess(res){
	if(res){
		message.success('计划开始');
	}
	return {type:START,res:res}
}
function endSuccess(res){
	if(res){
		message.success('计划结束');
	}
	return {type:END,res:res}
}
function terSuccess(res){
	if(res){
		message.success('计划终止');
	}
	return {type:TER,res:res}
}
function recoverySuccess(res){
	if(res){
		message.success('计划恢复');
	}
	return {type:RECOVERY,res:res}
}


function transportDateilsSuccess(data){
	return {type:DATEILS,data:data}
}
function addchuangboSuccess(data){
	return {type:ADDDATE,data:data}
}
function changboListSuccess(data){
    return {type:CHUANGBOLIST,data:data}
}


export function jhstart(panIds){
	let data ={
        planIds:panIds
	}
	return dispatch=>{
		post('/transportPlan/startTransportPlan.do',data)
			.then(res=>{
					dispatch(startSuccess(res.SUCCESS));
			})		
	}
	
}

export function end(panIds){
	let data ={
        planIds:panIds
	}
	return dispatch=>{
		post('/transportPlan/endTransportPlan.do',data)
			.then(res=>{
					dispatch(endSuccess(res.SUCCESS));
			})		
	}
	
}


export function cjTer(panIds){
	let data ={
        planIds:panIds
	}
	return dispatch=>{
		post('/transportPlan/stopTransportPlan.do',data)
			.then(res=>{
					dispatch(terSuccess(res.SUCCESS));
			})		
	}
	
}


export function recovery(panIds){
	let data ={
        planIds:panIds
	}
	return dispatch=>{
		post('/transportPlan/restoreTransportPlan.do',data)
			.then(res=>{
					dispatch(recoverySuccess(res.SUCCESS));
			})		
	}
	
}

export function chuangboList(dunWei,chuFaDi){
	let data ={
        dunWei:dunWei,
        chuFaDi:chuFaDi
	}
	return dispatch=>{
		post('/transportPlan/queryAvShip.do',data)
			.then(res=>{
					dispatch(changboListSuccess(res.shipList));
			})		
	}
	
}

export function tartsportdateilList(planId){
	let data ={
		planId:planId,
	}
	return dispatch=>{
		post('/transportPlan/queryTransportPlanDetails.do',data)
			.then(res=>{
					dispatch(transportDateilsSuccess(res));
			})		
	}
	
}

export function addChuang(planId,shipId,push){
	let data ={
        planId:planId,
        shipIds:shipId,
        push:push
	}
	return dispatch=>{
		post('/transportPlan/addTransportPlanShip.do',data)
			.then(res=>{
					dispatch(addchuangboSuccess(res.SUCCESS));
			})		
	}
	
}


